<template>
  <PageWrapper dense contentFullHeight fixedHeight>
    <a-card>
      <BasicTable @register="registerTable" :searchInfo="searchInfo">
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <TableAction
              :actions="[
                // {
                //   label: t('sys.script.details'),
                //   tooltip: '详细信息',
                //   onClick: handleView.bind(null, record),
                // },
                {
                  label: t('sys.configuration.edit'),
                  tooltip: t('sys.configuration.edit'),
                  onClick: handleCreate.bind(null, record),
                },
                {
                  label: t('sys.configuration.delete'),
                  color: 'error',
                  tooltip: t('sys.configuration.delete'),
                  popConfirm: {
                    title: t('sys.configuration.IsDelete'),
                    placement: 'left',
                    confirm: handleDelete.bind(null, record),
                  },
                },
              ]"
            />
          </template>
        </template>
      </BasicTable>
    </a-card>

    <AccountModal @register="registerModal" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { reactive, ref, computed, h } from 'vue';
  import { useModal } from '@/components/Modal';
  import { message } from 'ant-design-vue';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { getlistAPI, dataDeleteAPI } from '@/api/demo/disposition';
  import { PageWrapper } from '@/components/Page';
  import { columns, searchFormSchema } from './account.data';
  import { useGo } from '@/hooks/web/usePage';
  import AccountModal from './AccountModal.vue';
  import { PlusOutlined, SearchOutlined } from '@ant-design/icons-vue';
  import { useI18n } from '@/hooks/web/useI18n';

  const { t } = useI18n();
  defineOptions({ name: 'AccountManagement' });
  const total = ref(0);

  const tableTitle = computed(
    () =>
      `${t('sys.configuration.system')} ${t('sys.configuration.common')} ${total.value} ${t('sys.configuration.strip')}`,
  );
  const go = useGo();
  const searchInfo = reactive<Recordable>({});
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload, getForm, updateTableDataRecord }] = useTable({
    title: tableTitle,
    api: async (params) => {
      const result = (await getlistAPI(params)) as { items: any[]; total: number };
      total.value = result.total;
      return result;
    },
    rowKey: 'id',
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    actionColumn: {
      width: 120,
      title: t('sys.configuration.action'),
      dataIndex: 'action',
    },
  });
  const formState = ref({});
  const handleReset = () => {
    // formState.value = {};
    reload();
  };
  async function handleDelete(record: Recordable) {
    await dataDeleteAPI({ id: record.id });
    message.success('操作完成');
    reload();
  }

  const statusTypeList = ref([
    {
      title: t('sys.configuration.backstageService'),
      value: 1,
    },
    {
      title: 'MQ',
      value: 2,
    },
    {
      title: 'Redis',
      value: 3,
    },
    {
      title: 'Zookeeper',
      value: 4,
    },
  ]);
  function handleView(record: Recordable) {
    go('/system/account_detail/' + record.id);
  }

  function handleCreate(record) {
    if (record) {
      openModal(true, {
        isUpdate: true,
        record,
      });
    } else {
      openModal(true, {
        isUpdate: false,
      });
    }
  }

  function handleSuccess({ isUpdate, values }) {
    if (isUpdate) {
      const result = updateTableDataRecord(values.id, values);
    } else {
      reload();
    }
  }
</script>
